/**
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package snippet.theory4;

import java.io.*;

/**
 *
 * @author hoang.pt
 */
public class OutputStreamDemo {

    public OutputStreamDemo() {
    }


    public static void main(String[] a) throws IOException {

        //init String
        String tmp = "Hello c0909hv. This is test output";

        //make file
        try {
            //make file
            OutputStream outputStr = new FileOutputStream("test.txt");
            
            //serialize
            byte[] buff = tmp.getBytes();

            //write string into file
            outputStr.write(buff);

            //close file
            outputStr.close();

        } catch (FileNotFoundException ex) {
            //Logger.getLogger(OutputStreamDemo.class.getName()).log(Level.SEVERE, null, ex);
        } 

        //read file
        InputStream inputStr = new FileInputStream("test.txt");
        InputStream bufferStr = new BufferedInputStream(inputStr);

        //read each byte
        boolean eofFlag = false;

        while(!eofFlag) {
            int eofInt = bufferStr.read();
            System.out.println(eofInt);

            //until eof
            eofFlag = (eofInt == -1)? true:false;
        }

        //close file
        inputStr.close();
        bufferStr.close();
    }
}
